const _ = require('lodash')

function getArea (r) {
  console.log(r)
  return Math.PI * r * r
}

// let getAreaWithMemory = _.memoize(getArea)
// console.log(getAreaWithMemory(3));
// console.log(getAreaWithMemory(3));
// console.log(getAreaWithMemory(2));
// console.log(getAreaWithMemory(2));
// console.log(getAreaWithMemory(3));

function _memoize (f) {
  let cache = {}
  return function () {
    let key = JSON.stringify(arguments)
    cache[key] = cache[key] || f.apply(f, arguments)
    return cache[key]
  }
}

let getAreaWithMemory = _memoize(getArea)
console.log(getAreaWithMemory(3));
console.log(getAreaWithMemory(3));
console.log(getAreaWithMemory(2));
console.log(getAreaWithMemory(2));
console.log(getAreaWithMemory(3));
